package com.vn.ducktohome.screen.selectlevel;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.vn.ducktohome.constant.Constant;
import com.vn.ducktohome.enums.ScreenEnum;
import com.vn.ducktohome.gamelogic.GameControl;
/**
 * 
 * @author hpit
 *
 */
public class LevelBox extends Group{
	private boolean bOn;
	private int level;
	
	private Image imgBox, imgLevel, imgRed, imgLock;
	
	/**
	 * 
	 * @param bOn Level đã được mở hay chưa. true:mở, false:đóng.
	 * @param level
	 */
	public LevelBox(boolean bOn, int level) {
		TextureRegion tR;
		if (bOn)
			tR = new TextureRegion(GameControl.getAtlas().findRegion("select_level/select_on"));
		else
			tR = new TextureRegion(GameControl.getAtlas().findRegion("select_level/select_off"));
		imgBox = new Image(tR);
		this.addActor(imgBox);
		
		imgRed = new Image(GameControl.getAtlas().findRegion("select_level/red"));
		imgRed.setWidth(imgBox.getWidth());
		imgRed.setHeight(imgBox.getHeight());
		imgRed.setVisible(false);
		this.addActor(imgRed);
		
		this.level = level;
		imgLevel = new Image(GameControl.getAtlas().findRegion("select_level/select", level));
		this.addActor(imgLevel);
		
		if (!bOn){
			imgLock = new Image(GameControl.getAtlas().findRegion("select_level/lock"));
			this.addActor(imgLock);
		}
		
		initClick();
	}
	
	private void initClick(){
		imgLevel.addListener(new ClickListener(){

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				imgRed.setVisible(true);
				return super.touchDown(event, x, y, pointer, button);
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				imgRed.setVisible(false);
				Constant.ACTIVE_LEVEL = level;
				GameControl.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				GameControl.getScreenManager().getScreen().show();
				super.touchUp(event, x, y, pointer, button);
			}
		});
	}
}
